<template>
    <div class="user-edit all-height">
        <div :style="{ maxHeight: maxHeight }" class="drawer-content all-height">
            <div class="drawer-header">
                <div style="margin-right: 16px" v-show="type == '2'">添加区</div>
                <div style="margin-right: 16px" v-show="type != '2'">编辑区</div>
            </div>
            <Form ref="form" :model="form" :rules="formValidate" label-position="right" :label-width="100" class="marginForm">
                <Row :gutter="32">
                    <Col span="24">
                        <FormItem label="学校" prop="schoolCode">
                            <Select placeholder="请选择所属学校" v-model="form.schoolCode" @on-change="handleSchoolChange" filterable>
                                <Option v-for="item in schoolList" :value="item.code" :key="item.value">{{ item.name }}</Option>
                            </Select>
                        </FormItem>
                    </Col>
                </Row>
                <Row :gutter="32">
                    <Col span="24">
                        <FormItem label="模块" prop="module">
                            <Select placeholder="请选择所属模块" v-model="form.module" @on-change="handleModuleChange" filterable>
                                <Option v-for="item in moduleList" :value="item.value" :key="item.value">{{ item.title }}</Option>
                            </Select>
                        </FormItem>
                    </Col>
                </Row>
                <Row :gutter="32">
                    <Col span="24">
                        <FormItem label="学院审批" prop="collegeApproval">
                            <RadioGroup v-model="form.collegeApproval">
                                <Radio :label="true">开启</Radio>
                                <Radio :label="false">关闭</Radio>
                            </RadioGroup>
                        </FormItem>
                    </Col>
                </Row>
            </Form>
        </div>
        <div class="br right-bottom-edit" v-show="type != '0'">
            <Button type="primary" :loading="submitLoading" @click="submit">提交</Button>
            <Button @click="cancel">取消</Button>
        </div>
    </div>
</template>
<!--    </Drawer>-->
<!--  </div>-->
<!--</template>-->

<script>
// 根据你的实际请求api.js位置路径修改
import { addApprovalSettings, editApprovalSettings } from '@/views/bys/approval-settings/approvalSetting';
import { getDictDataByType } from '@/api/index';
export default {
    name: 'addEdit',
    components: {},
    props: {
        value: {
            type: Boolean,
            default: false
        },
        data: {
            type: Object
        },
        type: {
            type: String,
            default: '0'
        }
    },
    data() {
        return {
            moduleList: [],
            schoolList: [],
            visible: this.value,
            title: '',
            submitLoading: false,
            maxHeight: 510,
            form: {
                // 添加或编辑表单对象初始化数据
                schoolCode: '',
                schoolName: '',
                module: '',
                moduleName: '',
                collegeApproval: ''
            },
            // 表单验证规则
            formValidate: {
                schoolCode: [{ required: true, message: '请选择学校', trigger: 'blur' }],
                module: [{ required: true, message: '请选择模块', trigger: 'blur' }],
                collegeApproval: [{ required: true, message: '是否启用', trigger: 'blur', type: 'boolean' }]
            }
        };
    },
    methods: {
        init() {
            this.getDataDict();
        },
        cancel(e) {
            this.$emit('open-search', true);
        },
        submit() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.type == '1') {
                        // 编辑
                        this.submitLoading = true;
                        editApprovalSettings(this.form).then(res => {
                            this.submitLoading = false;
                            if (res.success) {
                                this.$Message.success('操作成功');
                                this.$emit('on-submit', true);
                                this.visible = false;
                            }
                        });
                    } else {
                        // 添加
                        this.submitLoading = true;
                        addApprovalSettings(this.form).then(res => {
                            this.submitLoading = false;
                            if (res.success) {
                                this.$Message.success('操作成功');
                                this.$emit('on-submit', true);
                                this.visible = false;
                            }
                        });
                    }
                }
            });
        },
        setCurrentValue(value) {
            if (value === this.visible) {
                return;
            }
            if (this.type == '1') {
                this.title = '编辑';
                this.maxHeight = Number(document.documentElement.clientHeight - 121) + 'px';
            } else if (this.type == '2') {
                this.title = '添加';
                this.maxHeight = Number(document.documentElement.clientHeight - 121) + 'px';
            } else {
                this.title = '详细信息';
                this.maxHeight = '100%';
            }
            // 清空数据
            this.$refs.form.resetFields();
            if (this.type == '0' || this.type == '1') {
                // 回显数据处理
                this.form = this.data;
            } else {
                // 添加
                delete this.form.id;
            }
            this.visible = value;
        },
        handleSchoolChange(value) {
            const selectedSchool = this.schoolList.find(item => item.code === value);
            if (selectedSchool) {
                this.form.schoolName = selectedSchool.name;
            }
        },
        handleModuleChange(value) {
            const selectedModule = this.moduleList.find(item => item.value === value);
            if (selectedModule) {
                this.form.moduleName = selectedModule.title;
            }
        },
        getDataDict() {
            this.postRequest('/schoolCode/getAll').then(res => {
                if (res.success) {
                    this.schoolList = res.result;
                }
            });

            getDictDataByType('business_form').then(res => {
                if (res.success) {
                    this.moduleList = res.result;
                }
            });
        }
    },
    watch: {
        value(val) {
            this.setCurrentValue(val);
        },
        visible(value) {
            this.$emit('input', value);
        }
    },
    mounted() {
        this.init();
    }
};
</script>

<style lang="less">
@import '../../../styles/drawer-common.less';
@import '../../personInfo.less';
.ivu-form-item {
    margin-bottom: 0.1rem;
}
.ivu-input-prefix {
    top: 0.03rem;
}
</style>
